{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# azure\n",
    "\n",
    "gpt-4o-realtime-preview\n",
    "\n",
    "gpt-4o-mini-realtime-preview\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv, find_dotenv\n",
    "import os\n",
    "load_dotenv(find_dotenv(), override=True)\n",
    "\n",
    "api_base = os.getenv(\"AZURE_OPENAI_ENDPOINT\")\n",
    "api_key = os.getenv(\"AZURE_OPENAI_API_KEY\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import AzureChatOpenAI\n",
    "model_name = \"gpt-4o-realtime-preview\"\n",
    "client = AzureChatOpenAI(\n",
    "            openai_api_version=\"2024-12-01-preview\",\n",
    "            api_version=\"2024-12-01-preview\",\n",
    "            azure_deployment=model_name,\n",
    "            temperature=0,\n",
    "            api_key=api_key,\n",
    "            azure_endpoint=api_base,\n",
    "            model_name=model_name\n",
    "        )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://learn.microsoft.com/en-us/azure/ai-services/openai/realtime-audio-quickstart?tabs=keyless%2Clinux&pivots=programming-language-python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%uv pip install openai[realtime]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import base64\n",
    "import asyncio\n",
    "from openai import AsyncAzureOpenAI\n",
    "\n",
    "async def main() -> None:\n",
    "    \"\"\"\n",
    "    When prompted for user input, type a message and hit enter to send it to the model.\n",
    "    Enter \"q\" to quit the conversation.\n",
    "    \"\"\"\n",
    "\n",
    "    client = AsyncAzureOpenAI(\n",
    "        azure_endpoint=os.environ[\"AZURE_OPENAI_ENDPOINT\"],\n",
    "        api_key=os.environ[\"AZURE_OPENAI_API_KEY\"],\n",
    "        api_version=\"2025-04-01-preview\",\n",
    "    )\n",
    "    async with client.beta.realtime.connect(\n",
    "        model=\"gpt-4o-realtime-preview\",  # deployment name of your model\n",
    "    ) as connection:\n",
    "        await connection.session.update(session={\"modalities\": [\"text\", \"audio\"]})  \n",
    "        while True:\n",
    "            user_input = input(\"Enter a message: \")\n",
    "            if user_input == \"q\":\n",
    "                break\n",
    "\n",
    "            await connection.conversation.item.create(\n",
    "                item={\n",
    "                    \"type\": \"message\",\n",
    "                    \"role\": \"user\",\n",
    "                    \"content\": [{\"type\": \"input_text\", \"text\": user_input}],\n",
    "                }\n",
    "            )\n",
    "            await connection.response.create()\n",
    "            async for event in connection:\n",
    "                if event.type == \"response.text.delta\":\n",
    "                    print(event.delta, flush=True, end=\"\")\n",
    "                elif event.type == \"response.audio.delta\":\n",
    "                    audio_data = base64.b64decode(event.delta)\n",
    "                    print(f\"Received {len(audio_data)} bytes of audio data.\")\n",
    "                elif event.type == \"response.audio_transcript.delta\":\n",
    "                    print(f\"Received text delta: {event.delta}\")\n",
    "                elif event.type == \"response.text.done\":\n",
    "                    print()\n",
    "                elif event.type == \"response.done\":\n",
    "                    break\n",
    "\n",
    "asyncio.run(main())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## gpt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import websockets\n",
    "import asyncio\n",
    "import json\n",
    "import pyaudio\n",
    "import wave\n",
    "from io import BytesIO\n",
    "\n",
    "# 配置参数\n",
    "API_KEY = \"your-azure-api-key\"\n",
    "ENDPOINT = \"wss://your-endpoint.openai.azure.com/openai/realtime\"\n",
    "DEPLOYMENT = \"gpt-4o-realtime-preview\"\n",
    "AUDIO_FORMAT = pyaudio.paInt16\n",
    "CHANNELS = 1\n",
    "RATE = 16000  # 16kHz采样率\n",
    "CHUNK = 1024\n",
    "\n",
    "# 初始化音频输入输出\n",
    "p = pyaudio.PyAudio()\n",
    "input_stream = p.open(format=AUDIO_FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)\n",
    "output_stream = p.open(format=AUDIO_FORMAT, channels=CHANNELS, rate=RATE, output=True)\n",
    "\n",
    "async def handle_audio():\n",
    "    async with websockets.connect(\n",
    "        f\"{ENDPOINT}?api-version=2024-10-01-preview&deployment={DEPLOYMENT}&api-key={API_KEY}\"\n",
    "    ) as ws:\n",
    "        # 1. 配置会话参数（语音风格、VAD检测等）\n",
    "        session_config = {\n",
    "            \"type\": \"session.update\",\n",
    "            \"session\": {\n",
    "                \"voice\": \"alloy\",  # 可选音色：alloy/coral/echo 等\n",
    "                \"input_audio_format\": \"pcm16\",\n",
    "                \"turn_detection\": {\n",
    "                    \"type\": \"server_vad\",\n",
    "                    \"threshold\": 0.4,\n",
    "                    \"silence_duration_ms\": 600\n",
    "                }\n",
    "            }\n",
    "        }\n",
    "        await ws.send(json.dumps(session_config))\n",
    "\n",
    "        # 2. 实时语音输入与处理\n",
    "        async def send_audio():\n",
    "            while True:\n",
    "                data = input_stream.read(CHUNK)\n",
    "                await ws.send(json.dumps({\n",
    "                    \"type\": \"input_audio_buffer.append\",\n",
    "                    \"data\": data.hex()  # PCM数据需转为16进制字符串\n",
    "                }))\n",
    "\n",
    "        # 3. 接收并播放语音输出\n",
    "        async def receive_audio():\n",
    "            async for message in ws:\n",
    "                msg = json.loads(message)\n",
    "                if msg.get(\"type\") == \"response.output_item.added\":\n",
    "                    audio_data = bytes.fromhex(msg[\"data\"][\"content\"])\n",
    "                    output_stream.write(audio_data)\n",
    "\n",
    "        # 并行处理输入与输出\n",
    "        await asyncio.gather(send_audio(), receive_audio())\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "asyncio.get_event_loop().run_until_complete(handle_audio())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "aiv2",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
